<?php
class model_conflict extends CI_Model {

	public function __construct()
	{
	//ini_set('memory_limit', '-1');
		$this->load->library('fclass');
		$this->load->database();
	}

	public function select($data)
	{
		$this->db->select('*');
		$this->db->from('tbl_customers');

		if(isset($data["cust_creator_id"])&&$data["cust_creator_id"]!="")
			$data["array_uId"][]= $data["cust_creator_id"];

		if(count($data["array_uId"])>0)
			$this->db->or_where_in('cust_creator_id', $data["array_uId"]); 

		if(isset($data["cust_status"])&&$data["cust_status"]!="")
			$this->db->where('cust_status =',$data["cust_status"]);



		if(isset($data["where"])&&$data["where"]!="")
			$this->db->where("(
								cust_first_name LIKE '%".$this->fclass->cleanup_text($data["where"])."%' 
								OR  cust_last_name LIKE '%".$this->fclass->cleanup_text($data["where"])."%' 
								OR cust_address LIKE '%".$this->fclass->cleanup_text($data["where"])."%'
								OR cust_phone LIKE '%".$this->fclass->cleanup_text($data["where"])."%'					
								)
							");

		$this->db->limit($data["limit"], $data["paging"]);
		//echo $this->db->last_query();
		$query = $this->db->get();
		return $query->result_array();
	}

	public function selectCount($data)
	{
		$this->db->select('*');
		$this->db->from('tbl_customers');

		if(isset($data["cust_creator_id"])&&$data["cust_creator_id"]!="")
			$data["array_uId"][]= $data["cust_creator_id"];

		if(count($data["array_uId"])>0)
			$this->db->or_where_in('cust_creator_id', $data["array_uId"]); 

		if(isset($data["cust_status"])&&$data["cust_status"]!="")
			$this->db->where('cust_status =',$data["cust_status"]);



		if(isset($data["where"])&&$data["where"]!="")
			$this->db->where("(
								cust_first_name LIKE '%".$this->fclass->cleanup_text($data["where"])."%' 
								OR  cust_last_name LIKE '%".$this->fclass->cleanup_text($data["where"])."%' 
								OR cust_address LIKE '%".$this->fclass->cleanup_text($data["where"])."%'
								OR cust_phone LIKE '%".$this->fclass->cleanup_text($data["where"])."%'					
								)
							");
		//echo $this->db->last_query();
		$query = $this->db->get();
		return $query->num_rows();	
	}

	public function selectCompanies($data)
	{
		$this->db->select('*');
		$this->db->from('tbl_companies');


		if(isset($data["comp_status"])&&$data["comp_status"]!="")
			$this->db->where('comp_status =',$data["comp_status"]);


		if(isset($data["where"])&&$data["where"]!="")
			$this->db->where("(
								comp_name LIKE '%".$this->fclass->cleanup_text($data["where"])."%' 
								OR comp_address LIKE '%".$this->fclass->cleanup_text($data["where"])."%'
								OR comp_phone LIKE '%".$this->fclass->cleanup_text($data["where"])."%'					
								)
							");

		$this->db->limit($data["limit"], $data["paging"]);
		//echo $this->db->last_query();
		$query = $this->db->get();
		return $query->result_array();
	}

	public function selectCountCompanies($data)
	{
		$this->db->select('*');
		$this->db->from('tbl_companies');


		if(isset($data["comp_status"])&&$data["comp_status"]!="")
			$this->db->where('comp_status =',$data["comp_status"]);


		if(isset($data["where"])&&$data["where"]!="")
			$this->db->where("(
								comp_name LIKE '%".$this->fclass->cleanup_text($data["where"])."%' 
								OR comp_address LIKE '%".$this->fclass->cleanup_text($data["where"])."%'
								OR comp_phone LIKE '%".$this->fclass->cleanup_text($data["where"])."%'					
								)
							");

		//echo $this->db->last_query();
		$query = $this->db->get();
		return $query->num_rows();	
	}
	
	public function selectOrder($data)
	{
		$this->db->select('*');
		$this->db->from('tbl_conflict_feeds');


		if(isset($data["cust_id"])&&$data["cust_id"]!="")
			$this->db->where('cust_id =',$data["cust_id"]);
			
		if(isset($data["type"])&&$data["type"]!="")
			$this->db->where('type =',$data["type"]);

		if(isset($data["array_cust_id"])&&count($data["array_cust_id"])>0)
			$this->db->or_where_in('cust_id', $data["array_cust_id"]); 
			
		$query = $this->db->get();
		return $query->result_array();	
	}

	
public function selectConflictTemp($data)
	{
	/*
		$this->db->select('*');
		$this->db->from('tbl_conflict_feeds');
		$this->db->join("tbl_customers","tbl_customers.cust_id = tbl_conflict_feeds.cust_id");

		if(isset($data["uId"])&&count($data["uId"])>0)
			$this->db->or_where_in('uId', $data["uId"]); 

		if(isset($data["id"])&&$data["id"]!="")
			$this->db->where('id', $data["id"]); 
	
		if(isset($data["order_by"])&&$data["order_by"]!="")
			$this->db->order_by($data["order_by"]); 

		if(isset($data["limit"])&&$data["limit"]!="")
			$this->db->limit($data["limit"], $data["paging"]);

		$query = $this->db->get();
		echo $this->db->last_query();
		return $query->result_array();	
		*/
		
		$this->db->select('*');
		$this->db->from('tbl_customers');

		if(isset($data["uId"])&&count($data["uId"])>0)
			$this->db->or_where_in('cust_creator_id', $data["uId"]); 

		
	
		if(isset($data["order_by"])&&$data["order_by"]!="")
			$this->db->order_by($data["order_by"]); 

		if(isset($data["limit"])&&$data["limit"]!="")
			$this->db->limit($data["limit"], $data["paging"]);

		$query = $this->db->get();
		//echo $this->db->last_query();
		return $query->result_array();	
	}
	
	public function selectConflict($data)
	{
		$this->db->select('*');
		$this->db->from('tbl_customers');

		if(isset($data["cust_conflict_id"])&&count($data["cust_conflict_id"])>0){
			$this->db->or_where_in('cust_conflict_id', $data["cust_conflict_id"]);
		}else{
			$this->db->or_where_in('cust_conflict_id', array(0));
		}			

		$this->db->where('cust_creator_id <> ',-1);
	
		if(isset($data["order_by"])&&$data["order_by"]!="")
			$this->db->order_by($data["order_by"]); 

		if(isset($data["limit"])&&$data["limit"]!="")
			$this->db->limit($data["limit"], $data["paging"]);

		$query = $this->db->get();
		//echo $this->db->last_query();
		return $query->result_array();	
	}

	public function insertConflict($data)
	{
		$this->db->trans_start();
		$this->db->insert('tbl_conflict_feeds', $data); 
		$insert_id = $this->db->insert_id();
   		$this->db->trans_complete();
   		return $insert_id;
		
	}

	public function updateCustUser($data,$where){
		if(isset($where["uId"])&&$where["uId"]!="")
			$this->db->where('uId', $where["uId"]);

		$this->db->where('cust_id', $where["cust_id"]);

		$this->db->update('tbl_cust_users', $data); 
		// /echo $this->db->last_query()."\n";
	}

	public function selectCustUser($data){		
		$this->db->select('*');
		$this->db->from('tbl_cust_users');
		if(isset($data["cust_id"])&&$data["cust_id"]!="")
			$this->db->where('cust_id', $data["cust_id"]);
		
		if(isset($data["uId"])&&$data["uId"]!="")
			$this->db->where('uId', $data["uId"]);
		$query = $this->db->get();
		return $query->row_array();	
	}

	

	public function selectCustUserFull($data){		
		$this->db->select('*');
		$this->db->from('tbl_cust_users');
		if(isset($data["cust_id"])&&$data["cust_id"]!="")
			$this->db->where('cust_id', $data["cust_id"]);
		if(isset($data["uId"])&&$data["uId"]!="")
			$this->db->where('uId', $data["uId"]);
		$query = $this->db->get();
		return $query->result_array();	
	}

	public function deleteConflict($data){
		$this->db->delete('tbl_conflict_feeds', $data); 
		// echo $this->db->last_query();
	}

}
	
?>